<?php
namespace Qgs\Controller;

class SetCommissionController extends BaseController
{

    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->local_id = session('userInfo')['local_id'];
    }

    /**
     *
     *  全国送佣金设置模块
     *
     */

    public function setCommission()
    {

        // 查询所有分类
        $local_id  = session('userInfo')['local_id'];
        $commisssList = $this->get_category_tree($local_id);
        $this->assign('local_id',$local_id);
        $this->assign('list',$commisssList);
        $this->display();

    }

    /**
     * 查询分类
     */

    public function get_category_tree($local_id)
    {
        $count = M('b2c_store_cat')->where(['parent_id' => 0,'local_id' => $local_id, 'disabled' => 'false'])->count();
        if ($count || $parent_id == 0) {
            /* 获取当前分类及其子分类 */
            $res = M('b2c_store_cat')->field('store_cat_id,parent_id,cat_name,disabled')
                ->where(['parent_id' => 0, 'local_id' => $local_id, 'disabled' => 'false'])
                ->order(['p_order' => 'asc', 'store_cat_id' => 'asc'])
                ->select();

            foreach ($res AS $row) {
                if ($row['disabled'] == 'false') {
                    $cat_arr[$row['store_cat_id']]['id'] = $row['store_cat_id'];
                    $cat_arr[$row['store_cat_id']]['name'] = $row['cat_name'];
                    $cat_goodsnum = M('b2c_goods')->where(['cat_id' => $row['store_cat_id']])->count();

                    if ($cat_goodsnum > 0) {
                        $num = M('qgs_cat_commission')->where(['local_id' => $local_id, 'cat_id' => $row['store_cat_id']])->getField('num');
                        if ($num === NULL) {
                            $num = C('DEFAULT_COMMISSION');
                        }
                        $cat_arr[$row['store_cat_id']]['num'] = $num;

                    }
                    if (isset($row['store_cat_id']) != NULL) {
                        $cat_arr[$row['store_cat_id']]['store_cat_id'] = $this->get_child_tree($row['store_cat_id'], $local_id);
                    }
                }
            }
        }
        if (isset($cat_arr)) {
            return $cat_arr;
        }
    }

    private function get_child_tree($tree_id = 0, $local_id)
    {
        $three_arr = array();

        $count = M('b2c_store_cat')->where(['parent_id' => $tree_id,'local_id' => $local_id, 'disabled' => 'false'])->count();
//        echo $count;
        if ($count || $tree_id == 0) {
            $res = M('b2c_store_cat')->field('store_cat_id,parent_id,cat_name,disabled')
                ->where(['parent_id' => $tree_id,'local_id' => $local_id, 'disabled' => 'false'])
                ->order(['p_order' => 'asc', 'store_cat_id' => 'asc'])
                ->select();

            foreach ($res AS $row) {
                if ($row['disabled'] == 'false') {

                    $three_arr[$row['store_cat_id']]['id'] = $row['store_cat_id'];
                    $three_arr[$row['store_cat_id']]['name'] = $row['cat_name'];
//->where(['cat_id' => $row['store_cat_id']])
                    $cat_goodsnum = M('b2c_goods')->count();
                    if ($cat_goodsnum > 0) {
                        $num = M('qgs_cat_commission')->where(['local_id' => $local_id, 'cat_id' => $row['store_cat_id']])->getField('num');
                        if ($num === NULL) {
                            $num = C('DEFAULT_COMMISSION');
                        }
                        $three_arr[$row['store_cat_id']]['num'] = $num;
                    }
                    if (isset($row['store_cat_id']) != NULL) {
                        $three_arr[$row['store_cat_id']]['store_cat_id'] = $this->get_child_tree($row['store_cat_id'], $local_id);
                    }
                }


            }
        }
        return $three_arr;
    }

    // 设置佣金值
    public function saveCommission()
    {     // 实例化模型
        $qgsCatCommission = M('qgs_cat_commission');

        if (!IS_AJAX) {
            $return = array(
                'status' => false,
                'message' => '非法请求',
            );
            $this->ajaxReturn($return);
        }
        $cat_id = I('post.cat_id');
        $num = I('post.num');
        $local_id = I('get.local_id');
        if (!$cat_id || !$local_id) {
            $return = array(
                'status' => false,
                'message' => '参数缺失',
            );
            $this->ajaxReturn($return);
        }
        if (is_array($cat_id)) {


            $qgsCatCommission->startTrans();
            foreach ($cat_id as $item) {
                $where = ['local_id' => $local_id, 'cat_id' => $item['cat_id']];
                $exists = $qgsCatCommission->where($where)->count();
//                $return = array(
//                    'status' => false,
//                    'message' => $exists,
//                );
//                $this->ajaxReturn($return);
//                exit;
                if ($exists) {
                    $res = $qgsCatCommission->where($where)->save(['num' => $item['num']]);
                } else {
                    $res = $qgsCatCommission->add(array_merge($where, ['num' => $item['num']]));
                }
                if ($res === false) {
                    $qgsCatCommission->rollback();
                    $return = array(
                        'status' => false,
                        'message' => '失败',
                    );
                    $this->ajaxReturn($return);
                }
            }

            $qgsCatCommission->commit();
            $return = array(
                'status' => true,
                'message' => '成功',
            );
            $this->ajaxReturn($return);
            die;
        } else {
            $where = ['local_id' => $local_id, 'cat_id' => $cat_id];
            if ($qgsCatCommission->where($where)->count()) {
                $res = $qgsCatCommission->where($where)->save(['num' => $num]);
            } else {
                $res = $qgsCatCommission->add(array_merge($where, ['num' => $num]));
            }
        }

        if ($res !== false) {
            $return = array(
                'status' => true,
                'message' => '成功',
                'data' => $res,
            );
        } else {
            $return = array(
                'status' => false,
                'message' => '失败',
            );
        }
        $this->ajaxReturn($return);
    }

}